<template>
  <el-dialog v-model="dialogVisible" title="批量修改" width="700" :before-close="handleClose">
    <el-select v-model="value" placeholder="请选择" clearable>
      <el-option label="修改上课时间" value="1" />
      <el-option label="修改授课地点" value="2" />
      <el-option label="修改上课老师" value="3" />
    </el-select>
    <div class="actionBtn" v-if="value">
      <TimeRange v-if="value == 1" format="HH:mm" v-model:start="classStartTime" v-model:end="classEndTime"></TimeRange>
      <dictSelect v-else-if="value == 2" v-model="classAddress" dictCode="teaching_location"></dictSelect>
      <el-input v-else v-model="teachingTeacher" placeholder="授课讲师" clearable />
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="save" :loading="loading">
          确 认
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
const emit = defineEmits(["fatherMethod"]);
const { proxy } = getCurrentInstance();
let dialogVisible = ref(false);
let value = ref("");
let classStartTime = ref("");
let classEndTime = ref("");
let classAddress = ref("");
let teachingTeacher = ref("");
let selectedRows = [];
let loading = ref(false);

function open(arr) {
  selectedRows = [...arr];
  dialogVisible.value = true;
}

async function save() {
  try {
    loading.value = true;
    if (value.value == 1) {
      if (!classStartTime.value)
        return proxy.$utils.showToast("请选择上课时间", 0);
    } else if (value.value == 2) {
      if (!classAddress.value)
        return proxy.$utils.showToast("请选择授课地点", 0);
    } else if (value.value == 3) {
      if (!teachingTeacher.value)
        return proxy.$utils.showToast("请选择授课讲师", 0);
    }
    for (let index = 0; index < selectedRows.length; index++) {
      const element = selectedRows[index];
      if (value.value == 1) {
        await proxy.$apis.teachingScheduleData({
          type: "update",
          data: {
            classStartTime: classStartTime.value,
            classEndTime: classEndTime.value,
            _id: element,
          },
        });
      } else if (value.value == 2) {
        await proxy.$apis.teachingScheduleData({
          type: "update",
          data: { classAddress: classAddress.value, _id: element },
        });
      } else {
        await proxy.$apis.teachingScheduleData({
          type: "update",
          data: { teachingTeacher: teachingTeacher.value, _id: element },
        });
      }
    }
    proxy.$utils.showToast("修改成功");
    handleClose();
  } catch (error) {
  } finally {
    loading.value = false;
  }
}

function handleClose() {
  value.value = "";
  classStartTime.value = "";
  classEndTime.value = "";
  classAddress.value = "";
  teachingTeacher.value = "";
  selectedRows = [];
  emit("fatherMethod");
  dialogVisible.value = false;
}

defineExpose({ open });
</script>



<style lang="scss" scoped>
.actionBtn {
  margin-top: 20px;
}
</style>